<?php

namespace app\common\model\kzvote;

use think\Model;


class Log extends Model
{

    

    

    // 表名
    protected $name = 'kzvote_log';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = false;
    protected $deleteTime = false;

    // 追加属性
    protected $append = [
        'type_text',
        'createtime_text',
    ];

    public function getTypeList()
    {
        return [1 => __('Type 1'), 2 => __('Type 2'), 3 => __('Type 3')];
    }

    public function getTypeTextAttr($value, $data){
        $value = $value ? $value : (isset($data['type']) ? $data['type'] : '');
        $list = $this->getTypeList();
        return isset($list[$value]) ? $list[$value] : '';
    }

    public function setJoinFormAttr($value)
    {
        if ($value){
            return json_encode($value);
        }else{
            return null;
        }
    }

    public function getJoinFormAttr($value)
    {
        if ($value) {
            return json_decode($value, true);
        } else {
            return null;
        }
    }
    public function getCreatetimeTextAttr($value, $data)
    {
        $value = $value ? $value : (isset($data['createtime']) ? $data['createtime'] : '');
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    public function vote()
    {
        return $this->belongsTo('Vote', 'vote_id', 'id', [], 'LEFT')->setEagerlyType(0)->field('name');
    }


    public function person()
    {
        return $this->belongsTo('Person', 'person_id', 'id', [], 'LEFT')->setEagerlyType(0)->field('name');
    }

    public function user()
    {
        return $this->belongsTo('app\\common\\model\\User', 'user_id', 'id', [], 'LEFT')->setEagerlyType(0)->field('nickname');
    }

}
